--[[ 编码: WMS-71-20 名称: 库存量表差异-库存量表差异检测 作者:HAN 日期:2025-1-29 级别:固定 (说明本段代码在项目中不太会变化) 函数: InventoryDiffCheck 功能: 后台处理脚本根据输入的仓库+库区重新计算 仓库、库区里的货品数量信息 1 -- 统计数据 (根据CG_Detai等表计算出来的数据) 2 -- 量表数据 3 -- 内存数据 更改记录: --]] wms_base = require ("wms_base") local function create_inventor_diff( strLuaDEID, check_obj_type, wh_code, area_code, zone_code ) local nRet, strRetInfo, code if ( check_obj_type == nil or check_obj_type == '' ) then return 1, "create_inventor_diff 函数中参数 check_obj_type 必须有值!" end if ( check_obj_type == "Warehouse" ) then if ( wh_code == nil or wh_code == '' ) then return 1, "create_inventor_diff 函数中参数 wh_code 必须有值!" end code = wh_code area_code = '' zone_code = '' elseif ( check_obj_type == "Area" ) then if ( wh_code == nil or wh_code == '' ) then return 1, "create_inventor_diff 函数中参数 wh_code 必须有值!" end if ( area_code == nil or area_code == '' ) then return 1, "create_inventor_diff 函数中参数 area_code 必须有值!" end code = area_code zone_code = '' elseif ( check_obj_type == "Zone" ) then if ( wh_code == nil or wh_code == '' ) then return 1, "create_inventor_diff 函数中参数 wh_code 必须有值!" end if ( area_code == nil or area_code == '' ) then return 1, "create_inventor_diff 函数中参数 area_code 必须有值!" end if ( zone_code == nil or zone_code == '' ) then return 1, "create_inventor_diff 函数中参数 zone_code 必须有值!" end code = zone_code else return 1, "create_inventor_diff 函数中参数 check_obj_type 值非法! "..check_obj_type end -- 根据CG_Detail 统计出库存量 nRet, strRetInfo = wms.wms_StatInventory( strLuaDEID, check_obj_type, code, 200) -- 返回 data_list: [ { "end_user": "", "item_code": "", "item_state": X, "item_route": "", "qty": X ,"qty_alloc":y" },...] if ( nRet ~= 0 ) then return 1, "wms_StatInventory: "..strRetInfo end if ( strRetInfo == '' ) then return 0 end local success, queryInfo, i success, queryInfo = pcall( json.decode, strRetInfo ) if ( success == false ) then return 1, "wms_StatInventory 返回结果啊非法的JSON格式!" end nPageCount = queryInfo.page_count nPage = 1 data_list = queryInfo.data_list -- 统计出来的数据集 local ret_info, parameter, diff, item_name local item_code, item_state, item_route, end_user, str_cls_id, str_condition local qty, alloc_qty, m_qty, m_alloc_qty, t_qty, t_alloc_qty while (nPage <= nPageCount) do for i = 1, #data_list do item_code = data_list[i].item_code item_state = data_list[i].item_state item_route = data_list[i].item_route end_user = data_list[i].end_user qty = data_list[i].qty alloc_qty = data_list[i].qty_alloc -- 获取内存中的量 m_qty = 0 m_alloc_qty = 0 parameter = {} parameter.end_user = end_user parameter.item_list = {} item = {} item.item_code = item_code item.item_state = item_state item.item_route = item_route parameter.item_list[1] = item str_condition = " S_ITEM_CODE = '"..item_code.."'" if ( check_obj_type == "Warehouse" ) then parameter.wh_code = wh_code nRet, strRetInfo = wms.wms_GetWHInventory( lua.table2str(parameter) ) if (nRet == 0) then ret_info = json.decode( strRetInfo ) m_qty = ret_info[1].qty_storage if ( qty ~= m_qty ) then diff = 'Y' end m_alloc_qty = ret_info[1].qty_alloc else return 1, "wms_GetWHInventory 失败!"..strRetInfo end str_cls_id = "WH_Inventory" str_condition = "S_WH_CODE = '"..wh_code.."' AND "..str_condition elseif (check_obj_type == "Area") then parameter.area_code = area_code nRet, strRetInfo = wms.wms_GetAreaInventory( lua.table2str(parameter) ) if (nRet == 0) then ret_info = json.decode( strRetInfo ) m_qty = ret_info[1].qty_storage m_alloc_qty = ret_info[1].qty_alloc else return 1, "wms_GetAreaInventory 失败!"..strRetInfo end str_cls_id = "AZ_Inventory" str_condition = "S_WH_CODE = '"..wh_code.."' AND S_AREA_CODE = '"..area_code.."' AND C_IS_LOGIC_AREA = 'N' AND "..str_condition else str_cls_id = "AZ_Inventory" str_condition = "S_WH_CODE = '"..wh_code.."' AND S_AREA_CODE = '"..area_code.."' AND C_IS_LOGIC_AREA = 'Y' AND "..str_condition end diff = 'N' if ( qty ~= m_qty ) then diff = 'Y' end if ( alloc_qty ~= m_alloc_qty ) then diff = 'Y' end -- 获取数据库里的量 local inventory err_msg = '' nRet, inventory = m3.GetDataObjByCondition(strLuaDEID, str_cls_id, str_condition ) if (nRet ~= 0) then if ( nRet == 1 ) then err_msg = str_cls_id.."量表中不存在该物料的存储表记录! 严重错误! 查询条件 = "..str_condition else lua.Error( strLuaDEID, debug.getinfo(1), "获取量表记录信息失败! " .. inventory) end item_name = "???" t_qty = 0 t_alloc_qty = 0 else item_name = inventory.item_name t_qty = inventory.qty t_alloc_qty = inventory.alloc_qty end if ( diff == 'N' ) then if ( qty ~= t_qty ) then diff = 'Y' end end if ( diff == 'N' ) then if ( alloc_qty ~= t_alloc_qty ) then diff = 'Y' end end local inventory_diff = m3.AllocObject(strLuaDEID,"WMS_Inventory_Diff") inventory_diff.wh_code = wh_code inventory_diff.area_code = area_code inventory_diff.zone_code = zone_code inventory_diff.item_code = item_code --inventory_diff.item_state = item_state --inventory_diff.item_route = item_route --inventory_diff.end_user = end_user inventory_diff.item_name = item_name inventory_diff.err_msg = err_msg inventory_diff.diff = diff inventory_diff.qty = qty inventory_diff.alloc_qty = alloc_qty inventory_diff.table_qty = t_qty inventory_diff.m_qty = m_qty inventory_diff.m_alloc_qty = m_alloc_qty inventory_diff.table_alloc_qty = t_alloc_qty nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, inventory_diff ) if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '创建【WMS_Inventory_Diff】对象失败!'..strRetInfo ) end end nPage = nPage + 1 if ( nPage <= nPageCount ) then -- 取下一页 nRet, strRetInfo = wms.wms_StatInventory( strLuaDEID, nPage) if ( nRet ~= 0 ) then return 1, "wms_StatInventory失败! nPage="..nPage.." "..strRetInfo end queryInfo = json.decode(strRetInfo) data_list = queryInfo.data_list end end return 0 end function InventoryDiffCheck ( strLuaDEID ) local nRet, strRetInfo local paramter nRet, paramter = m3.GetSysDataJson( strLuaDEID ) if ( nRet ~=0 ) then lua.Error( strLuaDEID, debug.getinfo(1), paramter ) end -- {"wh_code":"x","area_code":"x"} local wh_code = paramter.wh_code local area_code = paramter.area_code if ( wh_code == nil or wh_code == '') then lua.Error( strLuaDEID, debug.getinfo(1), "仓库编码必须有值! " ) end if ( area_code == nil ) then area_code = '' end if ( area_code ~= '' ) then nRet, strRetInfo = create_inventor_diff( strLuaDEID, "Area", wh_code, area_code ) if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo ) end else nRet, strRetInfo = create_inventor_diff( strLuaDEID, "Warehouse", wh_code ) if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo ) end end end